<template>
  <div class="table-box" style="height: auto">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户">
          <el-select
            clearable
            v-model="form.merchant_id"
            placeholder="请选择商户"
            filterable
          >
            <el-option
              v-for="item in merchantList"
              :key="item.value"
              :label="item.name + '-' + item.merchant_id"
              :value="item.merchant_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item>
          <el-input
            clearable
            placeholder="商户ID"
            v-model="form.merchant_id"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            clearable
            placeholder="商户名称"
            v-model="form.merchant_name"
          ></el-input>
        </el-form-item> -->
        <el-form-item label="审核结果">
          <el-select clearable v-model="form.audit_status">
            <el-option
              v-for="item in auditStatus"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            clearable
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table" style="height: auto">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column
            prop="audit_order_id"
            label="ID"
            align="center"
            width="60"
          />
          <el-table-column label="当前审核信息" align="left" width="240">
            <template #default="scope">
              <p>
                审核步骤：第 {{ scope.row.audit_level }} 审
                <el-button
                  type="primary"
                  size="small"
                  @click="handleOperate(scope.$index, scope.row, 'view')"
                  >查看</el-button
                >
              </p>
              <p>审 核 组：{{ scope.row.group_name }}</p>
              <p v-if="scope.row.audit_status == 1">
                审核人员：{{ scope.row.audit_user }}
              </p>
              <p>
                审核状态：<el-tag
                  :type="
                    scope.row.audit_status == 1
                      ? 'success'
                      : scope.row.audit_status == 2
                      ? 'danger'
                      : 'warning'
                  "
                >
                  {{ scope.row.audit_status_name }}
                </el-tag>
              </p>
            </template>
          </el-table-column>
          <el-table-column
            prop="createtime"
            label="添加时间"
            :formatter="formatterTime"
            align="center"
            width="85"
          ></el-table-column>
          <el-table-column
            prop="merchant_id"
            label="商户id"
            align="center"
            width="110"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
          />
          <el-table-column
            prop="account_type"
            label="客户类型"
            align="center"
          />
          <el-table-column label="客户信息" align="left" width="300">
            <template #default="scope">
              <p>公司名称：{{ scope.row.company_name }}</p>
              <p>公司别名：{{ scope.row.company_alias }}</p>
              <p>经营范围：{{ scope.row.company_business }}</p>
              <p>个人姓名：{{ scope.row.real_name }}</p>
              <p>联系方式：{{ scope.row.contact }}</p>
              <p>网站url：{{ scope.row.website }}</p>
              <p>地址信息：{{ scope.row.address }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="收款人信息"
            align="left"
            width="200"
            style="padding-left: 15px"
          >
            <template #default="scope">
              <p>付款方式：{{ scope.row.pay_way }}</p>
              <p>收款国家：{{ scope.row.receive_country }}</p>
              <p>收款地区：{{ scope.row.receive_city }}</p>
              <p>收款人地址：{{ scope.row.receive_address }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="银行信息"
            align="left"
            width="300"
            style="padding-left: 5px"
          >
            <template #default="scope">
              <p>银行名称：{{ scope.row.bank_name }}</p>
              <p>银行账号：{{ scope.row.bank_card_number }}</p>
              <p>
                银行国家/城市：{{ scope.row.bank_country }}
                {{ scope.row.bank_city }}
              </p>
              <p>银行地址：{{ scope.row.bank_address }}</p>
              <p>Swift码：{{ scope.row.bank_swift }}</p>
              <p>IBAN码：{{ scope.row.bank_iban }}</p>
              <p>支持币种：{{ scope.row.currency }}</p>
            </template>
          </el-table-column>
          <el-table-column label="附件信息" align="center" width="200">
            <template #default="scope">
              <el-link
                target="_blank"
                style="margin-right: 10px"
                v-if="scope.row.file_data.company_certificate"
                :href="scope.row.file_data.company_certificate"
                :type="
                  scope.row.file_data.company_certificate ? 'success' : 'info'
                "
                >公司注册证书</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.latest_register"
                :href="scope.row.file_data.latest_register"
                :type="scope.row.file_data.latest_register ? 'success' : 'info'"
                >最新商业登记证</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.latest_report"
                :href="scope.row.file_data.latest_report"
                :type="scope.row.file_data.latest_report ? 'success' : 'info'"
                >最新年审报告</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.financial_statements"
                :href="scope.row.file_data.financial_statements"
                :type="
                  scope.row.file_data.financial_statements ? 'success' : 'info'
                "
                >最新财务报表/税表</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.stock_certificate"
                :href="scope.row.file_data.stock_certificate"
                :type="
                  scope.row.file_data.stock_certificate ? 'success' : 'info'
                "
                >所有持股10%以上股东的有效証件</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.invoice"
                :href="scope.row.file_data.invoice"
                :type="scope.row.file_data.invoice ? 'success' : 'info'"
                >INVOICE范本</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.company_organization"
                :href="scope.row.file_data.company_organization"
                :type="
                  scope.row.file_data.company_organization ? 'success' : 'info'
                "
                >公司组织架构</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.company_address"
                :href="scope.row.file_data.company_address"
                :type="scope.row.file_data.company_address ? 'success' : 'info'"
                >公司地址证明(近3个月)</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.bank_statement"
                :href="scope.row.file_data.bank_statement"
                :type="scope.row.file_data.bank_statement ? 'success' : 'info'"
                >银行对账单(近3个月)</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.supplier_agreement"
                :href="scope.row.file_data.supplier_agreement"
                :type="
                  scope.row.file_data.supplier_agreement ? 'success' : 'info'
                "
                >供应商协议及发票(有效期内)</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.sale_agreement"
                :href="scope.row.file_data.sale_agreement"
                :type="scope.row.file_data.sale_agreement ? 'success' : 'info'"
                >销售协议及发票(有效期内)</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.kaihu_zhengjian"
                :href="scope.row.file_data.kaihu_zhengjian"
                :type="scope.row.file_data.kaihu_zhengjian ? 'success' : 'info'"
                >开户授权签字人有效证件+签字印鉴</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.kaihu_zhengjian_addr"
                :href="scope.row.file_data.kaihu_zhengjian_addr"
                :type="
                  scope.row.file_data.kaihu_zhengjian_addr ? 'success' : 'info'
                "
                >开户授权签字人个人地址证明(近3个月)</el-link
              >
              <el-link
                target="_blank"
                style="margin: 0 10px"
                v-if="scope.row.file_data.board_member_address"
                :href="scope.row.file_data.board_member_address"
                :type="
                  scope.row.file_data.board_member_address ? 'success' : 'info'
                "
                >董事地址证明(近3个月)</el-link
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="mer_review_status"
            label="商户审核"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.mer_review_status == 1
                    ? 'success'
                    : scope.row.mer_review_status == 2
                    ? 'danger'
                    : 'warning'
                "
              >
                {{ scope.row.mer_review_status == 1 ? "已审核" : "未审核" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="sys_review_status"
            label="系统审核"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.sys_review_status == 1
                    ? 'success'
                    : scope.row.sys_review_status == 0
                    ? 'warning'
                    : 'danger'
                "
              >
                {{
                  scope.row.sys_review_status == 1
                    ? "已审核"
                    : scope.row.sys_review_status == 2
                    ? "拒绝"
                    : "未审核"
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="100"
          >
            <template #default="scope">
              <el-button
                type="danger"
                v-if="
                  scope.row.mer_review_status === 1 &&
                  scope.row.sys_review_status === 0 &&
                  scope.row.allow_review === 1
                "
                @click="handleOperate(scope.$index, scope.row, 'audit')"
                >审核</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :btnType="btnType"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
      :auditHistory="auditHistory"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import { formatterTime, pickerDefaultTime, timestamp } from "@/utils/utils";
import {
  reviewCustomerOrderApi,
  reviewCustomerAuditApi,
  reviewHistoryApi,
} from "@/api/ExAssistant/review";
import DialogForm from "./components/customerAudit.vue";
import { GlobalStore } from "@/store";

const globalStore = GlobalStore();
const merchantList = computed(() => globalStore.merchantList);
const loading = ref(false);
const tableData = ref([]);
const auditStatus = ref([]);
const auditHistory = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref("");
const btnLoading = ref(false);
const btnType = ref("");

const formData = {
  merchant_id: "",
  merchant_name: "",
  audit_status: "",
  timeData: [],
};
const form = reactive({ ...formData });
const dialogFormData = {
  audit_order_id: "",
  kyc_id: "",
  review_status: "",
  review_reason: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
//参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    merchant_name: form.merchant_name,
    audit_status: form.audit_status,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await reviewCustomerOrderApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    auditStatus.value = res.data.extra.audit_status;
  } catch (error) {
    loading.value = false;
  }
};
//查询
const onQuery = async () => {
  getTbaleData();
};
// 行操作
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "audit") {
    btnType.value = "audit";
    dialogTitle.value = "审核客户信息";
    Object.assign(dialogForm, {
      //...row
      audit_order_id: row.audit_order_id,
      kyc_id: row.id,
      review_status: "",
    });
    dialogVisible.value = true;
  } else if (type === "view") {
    btnType.value = "view";
    dialogTitle.value = "查看审核记录";
    dialogVisible.value = true;
  }
  const his = await reviewHistoryApi({ order_id: row.audit_order_id });
  auditHistory.value = his.data.results;
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    let res;
    res = await reviewCustomerAuditApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
